package com.guit.hrms.listener;

import com.guit.hrms.event.interview.InterviewCreatedEvent;
import com.guit.hrms.event.interview.InterviewDeletedEvent;
import com.guit.hrms.event.interview.InterviewUpdatedEvent;
import com.guit.hrms.manager.InterviewTaskManager;
import com.guit.hrms.po.interview.Interview;
import com.guit.hrms.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class InterviewEventListener {
    @Autowired
    private InterviewTaskManager taskManager;

    @Autowired
    private InterviewService interviewService;

    @EventListener
    public void handleOfferCreatedEvent(InterviewCreatedEvent event) {
        Interview interview = event.getInterview();
        taskManager.addTask(
                interview.getId(),
                interview,
                () -> interviewService.sendInterviewEmail(interview.getId())
        );
    }

    @EventListener
    public void handleInterviewUpdatedEvent(InterviewUpdatedEvent event) {
        Interview interview = event.getInterview();
        taskManager.addTask(
                interview.getId(),
                interview,
                () -> interviewService.sendInterviewEmail(interview.getId())
        );
    }

    @EventListener
    public void handleInterviewDeletedEvent(InterviewDeletedEvent event) {
        Interview interview = event.getInterview();
        taskManager.removeTask(interview.getId());
    }
}
